<?php
/**
 * Created by PhpStorm.
 * User: Tommy
 * Date: 2018/11/9
 * Time: 16:18
 */

namespace App\Controllers;


use App\Services\GlobalMaintainService;
use common\enum\CommonEnum;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class GlobalMaintainController extends BaseController
{

    /**
     * 维护厅主表单
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function getHallList(Request $request)
    {
        $this->validate($request, [
            'hall_id' => 'integer',
            'page'    => 'integer',
            'page_size'    => 'integer',
        ]);
        $value = $request->input();
        return GlobalMaintainService::getHalllist($value);
    }


    /**
     * 设置维护时间
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function updateMaintainTime(Request $request)
    {
        $this->validate($request, [
            'hall_id'    => 'required',
        ]);
        $hallId = $request->input('hall_id');
        $time   = $request->only('start_time', 'end_time');
        if (!empty($time['start_time']) && !empty($time['end_time'])) {
            if (time() > strtotime($time['end_time'])) {//当前时间大于结束时间
                error_exit(SysCode::GLOBAL_END_TIME_ERROR);
            }
            if (strtotime($time['start_time']) > strtotime($time['end_time'])) {//开始时间大于结束时间
                error_exit(SysCode::GLOBAL_START_TIME_ERROR);
            }
        } else {
            $time['start_time'] = null;
            $time['end_time']   = null;
        }
        return GlobalMaintainService::maintainTime($hallId, $time);
    }
}